package com.ylpms.agme.controller;

import com.ylpms.agme.entity.RegionEntity;
import com.ylpms.agme.entity.StationEntity;
import com.ylpms.agme.service.RegionService;
import com.ylpms.agme.service.StationService;
import com.ylpms.too.AjaxJson;
import com.ylpms.too.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.tools.jconsole.Tab;

import java.util.List;

@RestController
public class RegionController {

    @Autowired
    RegionService regionService;

    @Autowired
    StationService stationService;

    @RequestMapping(value = "/getRegionById")
    public AjaxJson getRegionById(Integer regionid){
        AjaxJson ajaxJson = new AjaxJson();
        RegionEntity regionEntity  = regionService.getRegionById(regionid);
        ajaxJson.setObj(regionEntity);
        return ajaxJson;
    }

    @RequestMapping(value = "/getRegionsByCropCode")
    public Table getRegionsByCropCode(String cropCode){
        Table table = new Table();
        List<RegionEntity> regionEntities = regionService.getRegionsByCropCode(cropCode);
        table.setData(regionEntities);
        table.setCount(regionEntities.size());
        return table;
    }

    @RequestMapping(value = "/getRegionStations")
    public Table getRegionStations(Integer regionid){
        Table table = new Table();
        RegionEntity regionEntity  = regionService.getRegionById(regionid);
        String stationCodes = regionEntity.getStations();
        String [] codeArr = stationCodes.split(",");
        String codes = "";
        for(int i=0;i<codeArr.length;i++){
            codes += "'"+ codeArr[i] +"',";
        }
        if(codes.indexOf(",") != -1) codes = codes.substring(0,codes.length()-1);

        List<StationEntity> stationEntities = stationService.getStationsByCodes(stationCodes);
        table.setData(stationEntities);
        table.setCount(stationEntities.size());
        return table;
    }

}
